#!/usr/bin/env bash

    ########################
    # local-replication.sh #
    ########################

# This program is part of Rutherford, an artificial intelligence for the
# Faridayix computer operating system.

# Copyright (C) 2009 Scott C. MacCallum
# scottmaccal@gmail.com
# http://code.google.com/p/faridayix/source/browse/trunk/shell/local-replication.sh

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

local_replication()
{
  local aware_device
   local trouble_shoot
    local hot_swappable
     local plugged_in
      local device_name
       local location
        local image_name
         local run_again

  while [ "$run_again" != "no" ]; do
  printf "\nI am aware of the following devices:\n\n"
   espeak -s 90 "I am aware of the following devices:"
    dmesg | grep "ad0:"
     dmesg | grep "hda"
      dmesg | grep "sd" | grep "byte"

  printf "\nAm I aware of the device that you would like me to replicate? "
   espeak -s 90 "Am I aware of the device that you would like me to replicate?"
    read aware_device

  if [ "$aware_device" == "no" ]; then
   printf "\nWould you like me to help you trouble-shoot the problem? "
    espeak -s 90 "Would you like me to help you trouble-shoot the problem?"
     read trouble_shoot
    
    if [ "$trouble_shoot" == "yes" ]; then
    printf "\nIs your device hot-swappable? "
     espeak -s 90 "Is your device hot-swappable?"
      read hot_swappable

      if [ "$hot_swappable" == "yes" ]; then
      printf "\nIs your hot-swappable device plugged-in? "
       espeak -s 90 "Is your hot-swappable device plugged-in?"
        read plugged_in
    
        if [ "$plugged_in" == "yes" ]; then
         printf "OK."
          espeak -s 90 "OK."
       
        else
        printf "\nPlease plug-in your hot-swappable device now.\n"
         espeak -s 90 "Please plug-in your hot-swappable device now."
        fi
      fi
    fi
  fi

  printf "\nWhat is the name of the device that you would like me to replicate? /dev/"
   espeak -s 90 "What is the name of the device that you would like me to replicate?"
    read device_name

  if [ -e "/dev/$device_name" ]; then
  printf "\n"

  else  
  printf "\nI am not aware of the device $device_name.\n"
   espeak -s 90 "I am not aware of the device $device_name."
  fi

  if [ -e "/dev/$device_name" ]; then
  printf "I am aware of the following mounted device locations:\n\n"
   espeak -s 90 "I am aware of the following mounted devices locations:"
    mount

  printf "\nAm I aware of the mounted device location that you would like me to save to? "
   espeak -s 90 "Am I aware of the mounted device location that you would like me to save to?"
    read aware_device

  printf "\nWhere do you want me to save the image file? /"
   espeak -s 90 "Where do you want me to save the image file?"
    read location

  if [ -e "/$location" ]; then
  printf "\nWhat do you want me to name the image file? "
   espeak -s 90 "What do you want me to name the image file?"
    read image_name
        
  printf "\nI will now start the replication of /dev/$device_name and save the image file $image_name to the /$location directory.\n\n"
   espeak -s 90 "I will now start the replication of device $device_name and save the $image_name file to the /$location directory."
    dd if=/dev/$device_name of=$location/$image_name
  
   else
   printf "\nI am not aware of the /$location directory.\n"
    espeak -s 90 "I am not aware of the /$location directory."
   fi   
  fi
  done
 exit 0
}
local_replication
#END OF FILE